<template>
    <div>
         <div class="headstyle">
            <el-row :gutter="20">
                <el-col :span="16">
                    <div class="grid-content bg-purple">
                    <span class="locsty"><i class="el-icon-location iconsty"></i>当前位置：<router-link to="/coursemanage">课程管理</router-link>>课程编辑</span>
                    </div>
                </el-col>        
                <el-col :span="8" class="loc-index">
                    <div class="grid-content bg-purple">
                        <el-button type="primary" plain @click="goTo('/coursemanage')" class="backbtn">返回</el-button>                   
                    </div>
                </el-col>
            </el-row>
        </div>
        <div class="contentstyle">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
              <el-form-item label="课程名称" prop="name">
                  <el-input v-model="ruleForm.name" class="inputEdit"></el-input>
              </el-form-item>
              <el-form-item label="课程组分类" prop="region">
                  <el-select v-model="ruleForm.region" placeholder="请选择课程组分类" class="selectsty" @change="getValue">
                      <el-option label="公开课" value="0"></el-option>
                      <el-option label="AZ岗" value="1"></el-option>
                      <el-option label="非AZ岗" value="2"></el-option>
                  </el-select>
              </el-form-item>            
              <el-form-item label="业务分类" prop="ywregion" v-show="ywshowflag">
                  <el-select v-model="ruleForm.ywregion" placeholder="请选择业务分类" class="selectsty">
                      <el-option label="管理平台(相当于非AZ岗里的公开课)" value="管理平台"></el-option>
                      <el-option label="华为业务" value="华为业务"></el-option>
                      <el-option label="网优业务" value="网优业务"></el-option>
                      <el-option label="中兴业务" value="中兴业务"></el-option>
                      <el-option label="工程业务" value="工程业务"></el-option>
                      <el-option label="代维业务" value="代维业务"></el-option>                 
                  </el-select>
              </el-form-item>  
              <el-form-item label="是否启用" prop="delivery">
                  <el-switch v-model="ruleForm.delivery"></el-switch>
              </el-form-item>
              <el-form-item>
                  <el-button type="primary btnsty" @click="submitForm('ruleForm')">下一步</el-button>
                  <el-button type="primary btnsty" @click="submitFormcourse('ruleForm')">确定</el-button>
                  <el-button type="primary btnsty" @click="goTo('/coursemanage')">关闭</el-button>
              </el-form-item>    
            </el-form>
        </div>
    </div>
</template>
<script>
export default {
  data() {
    return {
      id: this.$route.params.id,

      ruleForm: {
        name: "", //课程名称
        region: "", //课程组分类
        ywregion: "", //业务线分类
        scordend: "", //业务线标识
        delivery: true, //启用禁用
        examtime: 5
      },
      rules: {
        name: [
          { required: true, message: "请输入课程名称", trigger: "blur" }
          //{ min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        region: [
          { required: true, message: "请选择课程组类别", trigger: "change" }
        ]
      },
      ywshowflag: false
    };
  },
  mounted() {
    if (this.id != undefined) {
      this.$axios
        .get("/Course/" + this.id + "/V1")
        .then(Jsondata => {
          this.ruleForm.name = Jsondata.data.Cos_Name;
          this.ruleForm.region = Jsondata.data.Cos_scope.toString();
          this.ruleForm.ywregion = Jsondata.data.Cos_Group.toString();
          this.ruleForm.scordend = Jsondata.data.Cos_scope_end.toString();
          this.ruleForm.delivery = Jsondata.data.Cos_Flag;
          this.ruleForm.examtime = Jsondata.data.examtime;
          if (Jsondata.data.Cos_scope == 2) {
            this.ywshowflag = true;
          }
        })
        .catch(error => {});
    }
  },
  methods: {
    getValue: function(region) {
      if (region == 2) {
        this.ywshowflag = true;
      } else {
        this.ywshowflag = false;
      }
    },
    goTo(path) {
      this.$router.replace(path);
    },
    submitForm(formName) {
      if (this.id != undefined) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            if (this.ruleForm.ywregion == "管理平台") {
              this.ruleForm.scordend = 0;
            } else {
              this.ruleForm.scordend = 1;
            }
            var list = {
              Cos_Name: this.ruleForm.name,
              Cos_scope: this.ruleForm.region,
              Cos_scope_end: this.ruleForm.scordend,
              Cos_Group: this.ruleForm.ywregion,
              Cos_Flag: this.ruleForm.delivery,
              examtime: this.ruleForm.examtime
            };
            this.$axios
              .put("/Course/" + this.id + "/V4", list)
              .then(Jsondata => {
                if (Jsondata.data) {
                  var couid = this.id;
                  this.$router.push({
                    name: "editchapters",
                    params: { couid }
                  });
                } else {
                  this.$message({
                    type: "info",
                    message: "您的登录信息已失效，请重新登录！"
                  });
                  this.$router.push("/login");
                }
              })
              .catch(error => {});
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      } else {
        this.$refs[formName].validate(valid => {
          if (valid) {
            if (this.ruleForm.ywregion == "管理平台") {
              this.ruleForm.scordend = 0;
            } else {
              this.ruleForm.scordend = 1;
            }
            var list = {
              Cos_Name: this.ruleForm.name,
              Cos_scope: this.ruleForm.region,
              Cos_scope_end: this.ruleForm.scordend,
              Cos_Group: this.ruleForm.ywregion,
              Cos_Flag: this.ruleForm.delivery,
              examtime: this.ruleForm.examtime
            };
            this.$axios
              .post("/Course", list)
              .then(Jsondata => {
                if (Jsondata.data != -1) {
                  if (Jsondata.data != 0) {
                    var couid = Jsondata.data;
                    this.$router.push({
                      name: "editchapters",
                      params: { couid }
                    });
                  } else {
                    this.$message({
                      type: "error",
                      message: "添加失败，请重试！"
                    });
                  }
                }else{
                  this.$message({
                      type: "error",
                      message: "您的登录信息已失效，请重新登录！"
                  });
                  this.$router.push("/login");
                }
              })
              .catch(error => {});
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      }
    },
    submitFormcourse(formName) {
      if (this.id != undefined) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            if (this.ruleForm.ywregion == "管理平台") {
              this.ruleForm.scordend = 0;
            } else {
              this.ruleForm.scordend = 1;
            }
            var list = {
              Cos_Name: this.ruleForm.name,
              Cos_scope: this.ruleForm.region,
              Cos_scope_end: this.ruleForm.scordend,
              Cos_Group: this.ruleForm.ywregion,
              Cos_Flag: this.ruleForm.delivery,
              examtime: this.ruleForm.examtime
            };
            this.$axios
              .put("/Course/" + this.id + "/V4", list)
              .then(Jsondata => {
                if (Jsondata.data) {
                  this.$router.push("/coursemanage");
                } else{
                  this.$message({
                      type: "error",
                      message: "您的登录信息已失效，请重新登录！"
                  });
                  this.$router.push("/login");
                }
              })
              .catch(error => {});
          } else {
            //console.log("error submit!!");
            return false;
          }
        });
      } else {
        this.$confirm(
          "您还没有创建章节信息，请点击下一步继续！否则课程信息不会保存",
          "提示"
        ).then(() => {
          return false;
        });
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.ywshowflag = false;
    }
  }
};
</script>

<style scoped>
/*.loc-index {
  padding-left: 150px !important;
}*/
.backbtn {
  width: 100px;
}
.headstyle {
  border-bottom: 2px solid #e5e5e5;
  padding-bottom: 20px;
}
.locsty {
  font-size: 16px;
  font-family: "Microsoft YaHei";
}
.iconsty {
  color: #53b9fe;
  font-size: 22px;
}
a {
  text-decoration: none;
  color: black;
}
.contentstyle {
  width: 75%;
  padding: 40px;
  padding-left: 100px;
}
.inputEdit {
  width: 48%;
}
.selectsty {
  width: 48%;
}
.btnsty {
  width: 85px;
}
</style>

